const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({
  data: {
    imgUrl: app.globalData.imgurl,
    popShow: false,
    loginCode: 0,
    phone: 0,
    backForTokenUrl: '',
    opId: 0,
    userInfo: '',
    encryptedData: '',
    iv: '',
  },
  onLoad(e) {
    var that = this;
    var backForTokenUrl = e.backForTokenUrl;
    if (backForTokenUrl) {
      that.setData({
        backForTokenUrl: backForTokenUrl
      })
    }
  },
  getPhoneNumber: function (e) {
    this.setData({
      popShow: false
    })
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      // ---------
      let that = this;
      that.setData({
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      })
      that.bindMobile()

    } else {
      that.bindMobile()

    }
  },
  bindMobile() {
    var that = this;
    var data = {
      encryptedData: that.data.encryptedData,
        iv: that.data.iv,
        openid: wx.getStorageSync('openid'),
        session_key: wx.getStorageSync('sessionKeyToken'),
    }
    common.request(api.get_UserTel,data,'post').then((res) =>{
      if(res.status == 0){
      wx.setStorageSync('user_tel', res.data.purePhoneNumber)
        that.Edit_UserTel(res.data.purePhoneNumber)
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
        that.setData({
          purePhoneNumber: res.data.purePhoneNumber
        })
      }
    })
    // wx.request({
    //   url: api.get_UserTel,
    //   data: {
    //     encryptedData: that.data.encryptedData,
    //     iv: that.data.iv,
    //     openid: wx.getStorageSync('openid'),
    //     session_key: wx.getStorageSync('sessionKeyToken'),
    //   },
    //   method: 'POST',
    //   header: {
    //     "Content-Type": "application/json;charset=UTF-8",
    //     'token': wx.getStorageSync('token')
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     if (res.data.status == 1 ) {
    //       wx.showToast({
    //         title: res.data.message,
    //         icon: 'none'
    //       })
          
    //     } else {
    //       wx.showToast({
    //         title: res.msg,
    //         icon: 'none'
    //       })
    //       setTimeout(function () {
    //         wx.navigateBack({
    //           delta: 1
    //         })
    //       }, 3000)
    //     }
    //   },
    //   fail(err) {
    //     console.log(err)
    //   }
    // });
  },
  Edit_UserTel(purePhoneNumber){
    let that = this;
    let data = {
      user_id:wx.getStorageSync('user_id'),
      user_tel:purePhoneNumber
    }
    common.request(api.Edit_UserTel,data,'post').then((res) =>{
      if(res.status == 1){
        console.log('手机号成功')
      }
    })
  },
  getUserProfile() {
    var that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        console.log(res.userInfo)
        that.setData({
          userInfo: res.userInfo,
         
        })
        this.shouquan_login()
      },
      fail: (res) => {
        console.log('获取用户信息失败', res)
        wx.showModal({
          title: '警告',
          content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
          showCancel: false,
          confirmText: '返回授权',
          success: function (res) { // 用户没有授权成功，不需要改变 isHide 的值

            if (res.confirm) {
              console.log('用户点击了“返回授权”');
            }
          }
        });
      }

    })
  },
  shouquan_login:function(){
    let that = this;
    wx.login({
      success : (res) =>{
        console.log("denglu",res)
        that.getOpenid(res.code,that.data.userInfo)
      },
      fail : (res) =>{
        wx.showToast({
          title: res.errMsg,
        })
      }
    })
  },
  getOpenid:function(code,userInfo){
    let that = this;
    let data = {
      code:code,
      gender:userInfo.gender,
      nickName:userInfo.nickName,
      avatarUrl:userInfo.avatarUrl,
    }
    console.log("接口返回数据",userInfo)

    common.request(api.Login,data,"post").then((res) => {
        console.log("接口返回数据",res)
        if(res.status == 1){
          wx.setStorageSync("sessionKeyToken", res.returndata.session_key);
          wx.setStorageSync("openid", res.returndata.openid);
           wx.setStorageSync('user_id', res.userinfo.user_id)
           wx.setStorageSync('user_tel', res.userinfo.user_tel)
           wx.setStorageSync('user_img', res.userinfo.user_img)
           wx.setStorageSync('user_nickname', res.userinfo.user_nickname)
           this.setData({
            popShow: true
           })
           
        }else{
          wx.setStorageSync("sessionKeyToken", '');
          wx.setStorageSync("openid", res.returndata.openid);
           wx.setStorageSync('user_id', '')
           wx.setStorageSync('user_img', '')
           wx.setStorageSync('user_nickname', '')
        }
    })
  },
})